#!/usr/bin/env python
#-*- coding:utf-8 -*-

import logging
from shorp.core.interpreter import *
from shorp.shorp import Shorp

VERSION = '20100407'
logger = logging.getLogger('shorp.xmlinterpreter')

class XmlInterpreter(Interpreter):
    def __init__(self, rule):
        shorp = Shorp()
        shorp.add(rule)
        self.rule = shorp.run()
        #print self.rule
        
    def interpret(self, node, intent=0):
        childs = node.getChilds()
        nodeName = node.getAttr('+')
        cls = node.getAttr('.')        
        attrs = ""
        
        eles = ""
        
        if nodeName == "":
            intent = -1
        for child in childs:
            eles += self.interpret(child, intent+1)
            
        element = nodeName
        
        if element == "":
            xml = self.intent(intent) + "%s\n"%eles
        elif cls == "noele":
            xml = self.intent(intent) + "<%s %s />\n"%(element, attrs)
        else:
            xml = (self.intent(intent) + "<%s %s>\n%s" + self.intent(intent) + "</%s>\n")%(element, attrs, eles, element)
        
        return xml
        